//
//  WNHoomToolBar.swift
//  XLWeibo
//
//  Created by 风不会停息 on 2017/7/27.
//  Copyright © 2017年 风不会停息. All rights reserved.
//

import UIKit

class WNHoomToolBar: UIView {
    
    var statusViewModels : WNStatusViewModel?{
        didSet{
            retweetButton?.setTitle("转发" + (statusViewModels?.status?.reposts_count ?? "0"), for: .normal)
            commnetButton?.setTitle("评论" + (statusViewModels?.status?.comments_count ?? "0"), for: .normal)
            unlikeButton?.setTitle("赞" + (statusViewModels?.status?.attitudes_count ?? "0"), for: .normal)
        
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var retweetButton : UIButton?
    var commnetButton : UIButton?
    var unlikeButton : UIButton?

    
    //  添加控件设置约束
    private func setupUI() {
        
         retweetButton = addChildButton(imageName: "timeline_icon_retweet", title: "转发" )
         commnetButton = addChildButton(imageName: "timeline_icon_comment", title: "评论")
         unlikeButton = addChildButton(imageName: "timeline_icon_unlike", title: "赞" )
//        + (statusViewModels?.status?.comments_count ?? "0")
        
        print((statusViewModels?.status?.reposts_count ?? "0" ))
        //  添加约束
        retweetButton?.snp.makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commnetButton!)
        }
        commnetButton?.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo((retweetButton?.snp.trailing)!)
            make.width.equalTo(unlikeButton!)
        }
        unlikeButton?.snp.makeConstraints { (make) in
            make.top.trailing.bottom.equalTo(self)
            //  关键约束
            make.leading.equalTo((commnetButton?.snp.trailing)!)
        }
        
        //  第一条条线
        let firstLineView = addLineView()
        //  第二条条线
        let secondLineView = addLineView()
        
        //  添加约束
        firstLineView.snp.makeConstraints { (make) in
            make.centerX.equalTo(retweetButton!.snp.trailing)
            make.centerY.equalTo(retweetButton!)
        }
        secondLineView.snp.makeConstraints { (make) in
            make.centerX.equalTo(commnetButton!.snp.trailing)
            make.centerY.equalTo(commnetButton!)
        }
    }
    
    //  添加子按钮
    private func addChildButton(imageName: String, title: String ) -> UIButton {
        
        let button = UIButton()
        //  设置图片
        button.setImage(UIImage(named: imageName), for: .normal)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        //  设置文字
        button.setTitle(title , for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        
                print((statusViewModels?.status?.reposts_count ?? "0" ))
        //  取消高亮点击
        button.adjustsImageWhenHighlighted = false
        //  添加按钮
        addSubview(button)
        return button
    }
    
    //  添加竖线视图
    private func addLineView() -> UIImageView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        //  添加子视图
        addSubview(imageView)
        return imageView
    }
    
    

}
